package cz.kfad.ofrss.activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockActivity;
import cz.kfad.ofrss.R;
import cz.kfad.ofrss.backend.BackendService;
import cz.kfad.ofrss.backend.IBackend;
import cz.kfad.ofrss.common.ApplicationContext;
import cz.kfad.ofrss.model.Category;
import cz.kfad.ofrss.util.StringUtil;

public class AddCategoryActivity extends SherlockActivity {

    public static final String CATEGORY_ID_KEY = "CATEGORY_ID_KEY";

    private IBackend backend = BackendService.createBackend(this);

    private int categoryId;

    private boolean isForEditing;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_category);

        categoryId = getIntent().getIntExtra(CATEGORY_ID_KEY, -1);
        isForEditing = categoryId != -1;

        if (isForEditing) {
            final Category category = backend.findCategoryById(categoryId);

            findNameText().setText(category.getName());
            findKeyText().setText(category.getKey());

            setTitle(R.string.category_edit_title);
        } else {
            setTitle(R.string.category_add_title);
        }
        Button saveButton = (Button) findViewById(R.id.categorySaveButton);
        saveButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                onSaveClick();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        ApplicationContext appState = ((ApplicationContext) getApplicationContext());
        appState.setContextToAsynTasks(this);
    }

    private void onSaveClick() {
        final Category category = new Category();
        category.setName(findNameText().getText().toString());
        category.setKey(findKeyText().getText().toString());

        if (validateCategory(category)) {
            ApplicationContext appState = ((ApplicationContext) getApplicationContext());
            if (isForEditing) {
                category.setId(categoryId);
            }
            appState.saveOrUpdate(category, this);
            Toast.makeText(this, getString(R.string.newCategorySaved), Toast.LENGTH_LONG).show();

            finish();
        } else {
            Toast.makeText(AddCategoryActivity.this, getString(R.string.categoryDefinitionInvalid), Toast.LENGTH_LONG).show();
        }
    }

    private boolean validateCategory(Category category) {
        final String name = category.getName();
        return name != null && !StringUtil.isEmpty(name);
    }

    private EditText findNameText() {
        return (EditText) findViewById(R.id.categoryName);
    }

    private EditText findKeyText() {
        return (EditText) findViewById(R.id.categoryKey);
    }

}
